package com.abc.fast.server.user.service.security;

import com.abc.fast.security.service.CustomDynamicSecurityService;
import com.abc.fast.server.user.service.IPermsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.ConfigAttribute;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取所有的权限Map
 * 放在用户模块是因为安全模块没法引入permsService
 * @Author LiJunXi
 * @Date 2024-03-02 15:21
 */
@Slf4j
@Configuration
public class CustomSecurityConfig {

    @Autowired
    private IPermsService permsService;

    @Bean
    public CustomDynamicSecurityService dynamicSecurityService() {
        return new CustomDynamicSecurityService() {
            @Override
            public Map<String, ConfigAttribute> loadDataSource() {
                Map<String, ConfigAttribute> map = new ConcurrentHashMap<>();
                // 查询所有权限列表
                permsService.getList().stream().forEach(item -> {
                    map.put(item.getPath(), new org.springframework.security.access.SecurityConfig(item.getPerms()));
                });
                log.debug("用户所含权限列表:{}", map);
                return map;
            }
        };
    }

}
